<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>获取变量的浮点值</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.empty.html">« empty</a></li>
      <li style="float: right;"><a href="function.get-debug-type.html">get_debug_type »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.var.html">Variable handling 函数</a></li>
    <li>获取变量的浮点值</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.floatval" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">floatval</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.2.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">floatval</span> &mdash; <span class="dc-title">获取变量的浮点值</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.floatval-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>floatval</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type">float</span></div>

  <p class="para rdfs-comment">
   获取变量 <code class="parameter">value</code> 的 <span class="type">float</span> 值。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.floatval-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">value</code></dt>

     <dd>

      <p class="para">
       可以是任何标量类型。<span class="function"><strong>floatval()</strong></span> 不能用于对象，这样做会发出
       <strong><code>E_NOTICE</code></strong> 级别的错误并返回 1。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.floatval-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   指定变量的浮点值。空数组返回 0，非空数组返回 1。
  </p>
  <p class="para">
   字符串很可能返回 0，尽管这取决于字符串最左边的字符。适用<a href="language.types.float.html#language.types.float.casting" class="link">浮点数强制转换</a>的通用规则。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.floatval-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5273">
    <p><strong>示例 #1 <span class="function"><strong>floatval()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$var&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'122.34343The'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$float_value_of_var&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">floatval</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$float_value_of_var</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;122.34343<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5274">
    <p><strong>示例 #2 <span class="function"><strong>floatval()</strong></span> 最左边的字符为非数字的示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$var&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'The122.34343'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$float_value_of_var&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">floatval</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$float_value_of_var</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;0<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.floatval-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.boolval.html" class="function" rel="rdfs-seeAlso">boolval()</a> - 获取变量的布尔值</span></li>
    <li class="member"><span class="function"><a href="function.intval.html" class="function" rel="rdfs-seeAlso">intval()</a> - 获取变量的整数值</span></li>
    <li class="member"><span class="function"><a href="function.strval.html" class="function" rel="rdfs-seeAlso">strval()</a> - 获取变量的字符串值</span></li>
    <li class="member"><span class="function"><a href="function.settype.html" class="function" rel="rdfs-seeAlso">settype()</a> - 设置变量的类型</span></li>
    <li class="member"><a href="language.types.type-juggling.html" class="link">类型转换</a></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>